package com.yiyue.graduate.controller;

import com.yiyue.graduate.pojo.Comment;
import com.yiyue.graduate.pojo.CommentsDTO;
import com.yiyue.graduate.pojo.Discuss;
import com.yiyue.graduate.pojo.User;
import com.yiyue.graduate.service.CommentService;
import com.yiyue.graduate.service.DiscussService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author yiyuexiaofusheng
 * @date 2021/5/9
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    DiscussService discussService;
    @Resource
    CommentService commentService;

    /**
     * 讨论界面
     * @param model
     * @return
     */
    @GetMapping("/discuss")
    public String toDiscuss(Model model){
        List<Discuss> allDiscuss = discussService.getAllDiscuss();
        model.addAttribute("allDiscuss",allDiscuss);

        return "/comment/discuss";
    }

    @GetMapping("/comment")
    public String toComment(Model model,Long id){
        Discuss discuss = discussService.getDiscuss(id);
        model.addAttribute("discuss",discuss);
        List<Comment> comments = commentService.listChildrenComment(id);
        System.out.println(comments);
        for (Comment comment:comments){
            System.out.println(comment);
        }
        model.addAttribute("comments",comments);
        return "/comment/detailpage";
    }


//    @GetMapping("/comments/{discussId}")
//    public String comments(@PathVariable Long blogId, ModelMap modelMap) {
//        modelMap.addAttribute("comments", commentService.listChildrenComment(blogId));
//        return "/comment/detailpage :: commentList";
//    }

    @PostMapping("/comments")
    public String comments(CommentsDTO commentsDTO){
        Comment comment = new Comment();
        comment.setContent(commentsDTO.getContent());
        Discuss DiscussId = new Discuss();
        DiscussId.setId(commentsDTO.getDiscussId());
        comment.setDiscussId(DiscussId);
        Comment parentComment = new Comment();
        parentComment.setId(commentsDTO.getParentComment());
        comment.setParentComment(parentComment);
        User user = new User();
        user.setId(commentsDTO.getUserId());
        comment.setUserId(user);
        commentService.saveComment(comment);
        return "redirect:/comment/comment?id="+commentsDTO.getDiscussId();
    }
}
